﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AvatarHelper;
using Avatar.Model;

public partial class Admin_ADPrice : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btn_Add.Visible = true;
            panel_Add.Visible = false;
            panel_Edit.Visible = false;
            using(var _adc=DBHelper.GetDataContext())
            {
                DropDownList1.DataBind();
                GridView1.DataSource = _adc.ADPrice.Where(p => p.ADCategoryID == new Guid(DropDownList1.SelectedValue));
                GridView1.DataBind();
            }
        }
    }
    protected void btn_UpdateSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txt_PriceUpdate.Text)) throw new Exception("价格不能为空");
            
            using (var _adc = DBHelper.GetDataContext())
            {
                Button _b = sender as Button;
                var _id = new Guid(_b.CommandArgument);

                var _adPrice=_adc.ADPrice.Where(p=>p.ID==_id).FirstOrDefault();
                if(_adPrice==null)throw new Exception("价格设定不存在");

                _adPrice.Price = Convert.ToInt32(txt_PriceUpdate.Text);

                _adc.SubmitChanges();

                btn_Add.Visible = true;
                panel_Add.Visible = false;
                panel_Edit.Visible = false;
                GridView1.DataBind();
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "编辑广告价格出错:"+_e.Message;
        }
    }
    protected void btn_UpdateCancel_Click(object sender, EventArgs e)
    {
        txt_PriceAdd.Text = "";
        list_PeriodUpdate.Dispose();

        btn_Add.Visible = true;
        panel_Add.Visible = false;
        panel_Edit.Visible = false;
        GridView1.DataBind();
    }
    protected void btn_AddSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txt_PriceAdd.Text)) throw new Exception("价格不能为空");

            using (var _adc = DBHelper.GetDataContext())
            {
                var _price = string.IsNullOrEmpty(txt_PriceAdd.Text) ? 0 : Convert.ToInt32(txt_PriceAdd.Text);
                var _periodID = Convert.ToInt32(list_PeriodUpdate.SelectedValue);

                var _adPrice = _adc.ADPrice.Where(p => p.PeriodID == _periodID && p.ADCategoryID == new Guid(DropDownList1.SelectedValue)).FirstOrDefault();
                if (_adPrice != null) throw new Exception("已为此周期设定价格，不能重复设定。");
                _adPrice = new ADPrice();

                _adPrice.ID = Guid.NewGuid();
                _adPrice.Price = _price;
                _adPrice.PeriodID = _periodID;
                _adPrice.ADCategoryID = new Guid(DropDownList1.SelectedValue);

                _adc.ADPrice.InsertOnSubmit(_adPrice);
                _adc.SubmitChanges();

                txt_PriceAdd.Text = "";

                btn_Add.Visible = true;
                panel_Add.Visible = false;
                panel_Edit.Visible = false;
                
                GridView1.DataSource = _adc.ADPrice.Where(p => p.ADCategoryID == new Guid(DropDownList1.SelectedValue));
                GridView1.DataBind();
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "*" + _e.Message;
        }
    }
    protected void btn_Update_Click(object sender, EventArgs e)
    {
        using (var _adc = DBHelper.GetDataContext())
        {

            Button _b = sender as Button;
            var _id =new Guid(_b.CommandArgument);
            var _d = _adc.ADPrice.Where(p =>p.ID == _id).FirstOrDefault();
            if (_d != null)
            {
                txt_PriceUpdate.Text = _d.Price.ToString();
                btn_UpdateSubmit.CommandArgument = _d.ID.ToString();

                btn_Add.Visible = false;
                panel_Add.Visible = false;
                panel_Edit.Visible = true;
            }
        }
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = false;
        try
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                Button _b = sender as Button;
                var _id = new Guid(_b.CommandArgument);

                var _adPrice = _adc.ADPrice.Where(p => p.ID == _id).FirstOrDefault();

                if (_adPrice == null) throw new Exception("价格设定不存在");
                if (_adPrice.Order.Count > 0) throw new Exception("此价格设定下包含广告订单若干，不能删除。");

                _adc.ADPrice.DeleteOnSubmit(_adPrice);
                _adc.SubmitChanges();

                btn_Add.Visible = true;
                panel_Add.Visible = false;
                panel_Edit.Visible = false;
                GridView1.DataBind();
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "删除失败:" + _e.Message;
        }
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        btn_Add.Visible = false;
        panel_Add.Visible = true;
        panel_Edit.Visible = false;
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        btn_Add.Visible = true;
        panel_Add.Visible = false;
        panel_Edit.Visible = false;

        using (var _adc = DBHelper.GetDataContext())
        {
            GridView1.DataSource = _adc.ADPrice.Where(p => p.ADCategoryID == new Guid(DropDownList1.SelectedValue));
            GridView1.DataBind();
        }
    }
}
